package cn.springboot.erp.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.springboot.erp.annotation.Log;
import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Categories;
import cn.springboot.erp.enums.Operation;
import cn.springboot.erp.service.CategoriesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("categories")
@Api(tags = "商品分类")
public class CategoriesController {

    @Resource
    private CategoriesService categoriesService;

    @RequiresPermissions("base:categories:select")
    @Log(description = Operation.SELECT,model = "商品分类")
    @RequestMapping("page")
    @ApiOperation("数据分页")
    public ResponseResult listPage(@RequestBody QueryParams<Categories> queryParams){
        return categoriesService.listPage(queryParams);
    }

    @RequiresPermissions("base:categories:select")
    @Log(description = Operation.SELECT,model = "商品分类")
    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        Categories categories = categoriesService.getById(id);
        System.out.println("categories = " + categories);
        return ResponseResult.Success().data("categories",categories);
    }

    @RequiresPermissions("base:categories:delete")
    @Log(description = Operation.DELETE,model = "商品分类")
    @ApiOperation("根据id删除")
    @GetMapping("deleteById/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        categoriesService.removeById(id);
        return ResponseResult.Success().msg("删除成功");
    }


    @Log(description = Operation.ADDORUPDATE,model = "商品分类")
    @RequiresPermissions(value = {"base:categories:add","base:categories:update"})
    @ApiOperation("添加或编辑")
    @PostMapping("insertOrUpdate")
    public ResponseResult insertOrUpdate(@RequestBody Categories categories){
        // 获取商品名称
        String name = categories.getName();
        // 查询商品类型信息
        LambdaQueryWrapper<Categories> lambda = new QueryWrapper<Categories>().lambda();
        lambda.eq(Categories::getName,name);
        Categories one = categoriesService.getOne(lambda);

        System.out.println("one = " + one);
        System.out.println(ObjectUtils.isNotNull(one));
        // 如果one存在
        if (ObjectUtils.isNotNull(one)&& categories.getId()!=null){
            // 比较id，如果id相同则是同一个对象，做编辑操作
            if (ObjectUtil.equal(one.getId(),categories.getId())){
                categoriesService.saveOrUpdate(categories);
                return ResponseResult.Success().msg("编辑成功");
            }
            return ResponseResult.Success().msg("该商品分类已经存在");
        }
        // 如果不存在，做添加操作
        categoriesService.saveOrUpdate(categories);
        return ResponseResult.Success().msg("添加成功");
    }


}
